package top.jnaw.test.controller;

import com.alibaba.fastjson.JSONArray;
import com.jfinal.core.Controller;
import com.jfinal.kit.StrKit;
import java.util.List;
import lzj.tools.Https;
import top.jnaw.test.model.Dept;
import top.jnaw.test.model.UserDept;
import top.jnaw.test.service.DeptService;

public class DeptController extends Controller {


  /**
   * 添加一个部门
   */
  public void add() {
    final String name = getPara("name", "").trim();//部门名称
    final String bz = getPara("bz", "").trim();//备注，没用
    final int fid = getParaToInt("fid", 0);//父级部门id

    String msg = "";
    boolean result = false;

    if (StrKit.notBlank(name)) {
      Dept dept = new Dept();
      dept.setName(name);
      dept.setBz(bz);
      dept.setFid(fid);
      result = dept.save();
    } else {
      msg += "大队部门名不能为空！";
    }
    if (result) {
      renderJson(Https.success("添加成功", ""));
    } else {
      renderJson(Https.failure("添加失败！" + msg, ""));
    }
  }

  /**
   * 删除一个部门
   */
  public void rm() {
    final int id = getParaToInt("id", -1);
    boolean result = false;
    String msg = "";
    if (id >= 0) {
      Dept dept = Dept.dao.findById(id);
      if (null != dept) {
        result = dept.delete();
      } else {
        msg += "该部门不存在！";
      }
    }
    if (result) {
      renderJson(Https.success("删除成功", ""));
    } else {
      renderJson(Https.failure("删除失败！" + msg, ""));
    }
  }

  /**
   * 修改部门
   */
  public void set() {
    final int id = getParaToInt("id", -1);
    final String name = getPara("name", "").trim();
    final String bz = getPara("bz", "").trim();

    boolean result = false;
    String msg = "";
    if (id >= 0) {
      Dept dept = Dept.dao.findById(id);
      if (null != dept) {
        if (StrKit.notBlank(name)) {
          dept.setName(name);
        }
        dept.setBz(bz);
        result = dept.update();
      } else {
        msg += "该部门不存在！";
      }
    }

    if (result) {
      renderJson(Https.success("修改成功", ""));
    } else {
      renderJson(Https.failure("修改失败！" + msg, ""));
    }
  }

  /**
   * 全部的部门树
   */
  public void getListTree() {
    final int id = getParaToInt("deptid", 0);//获取到的是子集部门列表
    List<Dept> list = DeptService.getList();
    JSONArray deptjson = null;
    if (null != list) {
      deptjson = DeptService.getChildren(list, id, null);
//          json = all.getJSONObject(0).toJSONString();
    }
    renderJson(Https.success("查询成功", deptjson));
  }

  /**
   * 没用
   */
  public void getListTree2() {
    final String id = getPara("deptid", "0");//获取到的是子集部门列表
    renderJson(Https.success("查询成功", DeptService.getdeptsString(id)));
  }


  public void addUserToDept(){
    final int uid = getParaToInt("uid", -1);
    final int deptid = getParaToInt("deptid", -1);
    boolean res = false;
    UserDept userDept = new UserDept();
    userDept.setDeptId(uid);
    userDept.setDeptId(deptid);
    res = userDept.save();
    if (res){
      renderJson(Https.success("分配成功",""));
    }else {
      renderJson(Https.failure("分配失败",""));
    }


  }
}


